//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The core libraries for the MapWindow 6.0 project.
//
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is MapWindow.dll for the MapWindow 6.0 project
//
// The Initial Developer of this Original Code is Ted Dunsford. Created in January, 2008.
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

using MapWindow.Main;
using MapWindow.Data;
using MapWindow.Geometries;
using MapWindow.Drawing;


using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;



namespace MapWindow.Components
{
    /// <summary>
    /// Creates a set of line segments and appropriate vertex buffers for the process.
    /// </summary>
    public class oldLineLayer
    {
        private Microsoft.DirectX.Direct3D.VertexBuffer _VetexBuffer;
        private int _SegmentCount;
        private List<Vector3> _Segs;
        private Color _LineColor;
        private bool _Initialized;
        

        /// <summary>
        /// Creates a new mechanism for tracking linear features like roads or rivers for the 3D environment
        /// </summary>
        /// <param name="fl">Any valid IFeatureLayer</param>
        /// <param name="device">A direct3D device</param>
        public oldLineLayer(IFeatureSet fl, Device device)
        {
            if (fl.FeatureType != FeatureTypes.Line)
            {
                throw new ArgumentException("LineLayer should be created from a line feature");
            }
            oldLineLayer iml = fl as oldLineLayer;
            if(iml != null)
            {
               // _LineColor = iml.FillPens[0].Color;
            }
            else
            {
                _LineColor = Color.Blue;
            }

            _Segs = new List<Vector3>();
            foreach (IFeature f in fl.Features)
            {

                for (int i = 0; i < f.BasicGeometry.NumGeometries; i++)
                {
                    IBasicGeometry bg = f.BasicGeometry.GetBasicGeometryN(i);
                    Vector3[] vec = new Vector3[bg.NumPoints];
                    for (int iCoord = 0; iCoord < bg.NumPoints; iCoord++)
                    {
                        vec[iCoord] = new Vector3((float)(bg.Coordinates[iCoord].X), (float)(bg.Coordinates[iCoord].Y), 0f);
                        if (iCoord > 0)
                        {
                            _Segs.Add(vec[iCoord - 1]);
                            _Segs.Add(vec[iCoord]);
                        }
                    }
                }
            }
            _SegmentCount = _Segs.Count / 2;

            _VetexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), _Segs.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            _VetexBuffer.Created += new EventHandler(VetexBuffer_created);
            VetexBuffer_created(_VetexBuffer, null);
        }

       
        void VetexBuffer_created(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;

            // Create a list of vertices equal to the number of items
            CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[_Segs.Count];

            
            for (int i = 0; i < _Segs.Count; i++)
            {
                verts[i].Position = _Segs[i];
                verts[i].Color = _LineColor.ToArgb();
            }
            buffer.SetData(verts, 0, LockFlags.None);

        }

        private void Initialize(Device device, IProgressHandler progressHandler)
        {
            _Initialized = true;

        }

       

         /// <summary>
        /// Actually renders the contents of this LineLayer onto the specified Direct3D device
        /// </summary>
        /// <param name="device">The device to draw is LineLayer to</param>
        public void Draw(object device)
        {
            Draw(device, null);
        }
        

        /// <summary>
        /// Actually renders the contents of this LineLayer onto the specified Direct3D device
        /// </summary>
        /// <param name="device">The Microsoft.DirectX.Direct3D.Device to draw is LineLayer to</param>
        /// <param name="progressHandler">Any valid implementation of the IProgressHandler interface for receiving progress messages </param>
        public void Draw(object device, IProgressHandler progressHandler)
        {
            Device dev = device as Device;
            if (dev == null)
            {
                return;
            }
            if (_Initialized == false)
            {
                Initialize(dev, progressHandler);
            }
            if (_SegmentCount > 0)
            {
                dev.VertexFormat = CustomVertex.PositionColored.Format;
                dev.RenderState.PointSize = 3f;
                dev.SetStreamSource(0, _VetexBuffer, 0);
                dev.DrawPrimitives(PrimitiveType.LineList, 0, _SegmentCount);
            }
        }

        /// <summary>
        /// Gets or sets the line color to use for this layer
        /// </summary>
        public Color LineColor
        {
            get { return _LineColor; }
            set 
            {
                if (value != _LineColor) // don't waste time if this is just the same value
                {
                    _LineColor = value; 
                    VetexBuffer_created(_VetexBuffer, null);
                }
                
                
            }
        }
    }

}
